package com.my.netty.promote.server.handler;

import com.my.netty.promote.message.GroupQuitRequestMessage;
import com.my.netty.promote.message.GroupQuitResponseMessage;
import com.my.netty.promote.server.session.GroupSession;
import com.my.netty.promote.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable
public class GroupQuitRequestMessageHandler extends SimpleChannelInboundHandler<GroupQuitRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupQuitRequestMessage message) throws Exception {
        String groupName = message.getGroupName();
        String username = message.getUsername();
        //暂不做存在校验
        GroupSession groupSession = GroupSessionFactory.getSession();
        groupSession.removeMember(groupName, username);
        GroupQuitResponseMessage responseMessage = new GroupQuitResponseMessage(true, "你已退出" + groupName);
        ctx.writeAndFlush(responseMessage);
    }
}
